package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.ArchiveBox;
import cn.stylefeng.guns.modular.basic.entity.ArchiveLayer;
import cn.stylefeng.guns.modular.basic.mapper.ArchiveLayerMapper;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveBoxParam;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveLayerParam;
import cn.stylefeng.guns.modular.basic.model.result.ArchiveLayerResult;
import cn.stylefeng.guns.modular.basic.service.ArchiveBoxService;
import cn.stylefeng.guns.modular.basic.service.ArchiveLayerService;
import cn.stylefeng.guns.modular.manager.entity.ArchiveInfo;
import cn.stylefeng.guns.modular.manager.service.ArchiveInfoService;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 档案层信息 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-17
 */
@Service
public class ArchiveLayerServiceImpl extends ServiceImpl<ArchiveLayerMapper, ArchiveLayer> implements ArchiveLayerService {

    @Autowired private ArchiveInfoService archiveInfoService;
    @Autowired
    private ArchiveBoxService archiveBoxService;
    @Override
    public boolean hasArchiveInfo(String key) {
        final List<String> boxids = this.archiveBoxService.lambdaQuery()
                .eq(ArchiveBox::getSsdagcid, key)
                .select(ArchiveBox::getId)
                .list().parallelStream().map(ArchiveBox::getId).collect(Collectors.toList());

       return this.archiveInfoService.lambdaQuery()
               .in(ArchiveInfo::getDahid,boxids)
               .count() > 0;
    }

    @Override
    public void add(ArchiveLayerParam param) {
        ArchiveLayer entity = getEntity(param);
        this.save(entity);
    }


    @Override
    @Transactional
    public void delete(ArchiveLayerParam param) {
        final Serializable key = getKey(param);
        boolean isSuccess = this.removeById(key);
        String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess,TIPS);

        final List<ArchiveBox> list = this.archiveBoxService.lambdaQuery()
                .select(ArchiveBox::getId)
                .eq(ArchiveBox::getSsdagcid, key)
                .list();

        for (ArchiveBox archiveBox : list) {
            ArchiveBoxParam archiveBoxParam = new ArchiveBoxParam();
            archiveBoxParam.setId(archiveBox.getId());
            this.archiveBoxService.delete(archiveBoxParam);
        }
    }

    @Override
    public void update(ArchiveLayerParam param) {
        ArchiveLayer oldEntity = getOldEntity(param);
        ArchiveLayer newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveLayerResult findBySpec(ArchiveLayerParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ArchiveLayerResult> findListBySpec(ArchiveLayerParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveLayerParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveLayerParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveLayer getOldEntity(ArchiveLayerParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveLayer getEntity(ArchiveLayerParam param) {
        ArchiveLayer entity = new ArchiveLayer();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<ZTreeNode> tree(ArchiveLayerParam param) {
        return this.baseMapper.tree(param);
    }

    @Override
    public String getMaxCode(String ssdasid, String ssdagid) {
        return this.baseMapper.getMaxCode(ssdasid,ssdagid);
    }
}
